﻿USE [ncc-sop]
GO
/****** Object:  StoredProcedure [dbo].[getLVL2TableID4]    Script Date: 02/20/2014 11:17:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[getLVL2TableID4]
	-- Add the parameters for the stored procedure here
	@statsType varchar(30), @aggregateType varchar(30)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	select distinct MetadataDPMTables.TableId, MetadataDPMTables.rptName  from MetadataDPMTableAttributeValues inner join MetadataDPMTables on MetadataDPMTableAttributeValues.TableId = MetadataDPMTables.TableId 
		where MetadataDPMTables.TableId in ( 
		select distinct TableId from MetadataDPMTableAttributeValues 
		where TableId in (
			select MetadataDPMTables.TableId from 
			MetadataDPMTables inner join MetadataDPMTableAttributeValues on MetadataDPMTables.TableId = MetadataDPMTableAttributeValues.TableId 
			where TableAttributeId = 1 and TableAttributeValue=@statsType
			) 
		) 
	and TableAttributeId = 2 and TableAttributeValue = @aggregateType
END
